﻿using System;
using System.Reflection;
using System.Collections;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Text;


namespace iSun.DBModule
{
    /// <summary>
    /// 说明：实体基类
    /// 
    /// </summary>
    [Serializable]
    public class BaseEntity
    {
        /// <summary>
        /// 數據複製，把自身複製一份。
        /// </summary>
        /// <returns>返回被複製的對象</returns>
        public virtual BaseEntity Clone()
        {
            Type theType = this.GetType();
            BaseEntity Result = (BaseEntity)Activator.CreateInstance(theType);
            PropertyInfo[] Mypropertyinfos =  this.GetType().GetProperties();
            foreach(PropertyInfo proInfo in Mypropertyinfos)
            {   
                PropertyInfo prop = theType.GetProperty(proInfo.Name);
                if (prop == null)
                    continue;
                if (prop.CanWrite && prop.CanRead)
                {
                    object value1 = prop.GetValue(this, null);
                    if (value1 != null)
                    {
                        if (prop.PropertyType.GetInterface(typeof(ICollection).ToString(), true) == null)
                        {
                            object temp = this;
                            //member.StorageAccessor.SetBoxedValue(ref temp, value1);
                            prop.SetValue(Result, value1, null);
                        }
                    }
                }
            }
            return Result;
        }

        /// <summary>
        /// 数据置NULL
        /// </summary>
        /// <param name="AObject"></param>
        /// <param name="SetToNull"></param>
        public virtual void ClearData(BaseEntity AObject, bool SetToNull)
        {
            if (SetToNull)
            {
                AObject = null;
            }
        }

        /// <summary>
        /// 数据验证[]
        /// </summary>
        /// <returns>返回数据验证结果</returns>
        public virtual string CheckData()
        {
            return "";
        }


        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// 对象比较
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Equals(BaseEntity obj)
        {
            if (!(obj is BaseEntity))
                return base.Equals(obj);

            if (obj.GetType() != this.GetType())
                return false;

            BaseEntity ASource = obj as BaseEntity;

            Type theType = this.GetType();

            object[] Attr = theType.GetCustomAttributes(typeof(TableAttribute), true);
            if (Attr.Length == 0)
                return false;
            PropertyInfo[] Mypropertyinfos = this.GetType().GetProperties();
            foreach (PropertyInfo proInfo in Mypropertyinfos)
            { 
                PropertyInfo prop = theType.GetProperty(proInfo.Name);

                Object Value1 = prop.GetValue(this, null);
                Object Value2 = prop.GetValue(ASource, null);
                if ((Value1 == null && Value2 != null) || (Value1 != null && Value2 == null))
                    return false;

                if (Value1 != null)
                {
                    if (!Value1.Equals(Value2))
                        return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 获取当前实体对应的表名
        /// </summary>
        /// <returns></returns>
        public string GetTableName()
        {
            var entity = (this as DataTableBaseEntity);
            if (entity != null)
            {
                return entity.TableName;
            }
            Type t = this.GetType();
            foreach (Attribute attr in t.GetCustomAttributes(true))
            {
                TableAttribute tableattribute = attr as TableAttribute;
                if (null != tableattribute)
                {
                    return tableattribute.TableName;
                }
            }
            return "";
        }


        /// <summary>
        /// 获取当前实体对应Guid？
        /// </summary>
        /// <returns></returns>
        public virtual string GetTableId()
        {
            var entity = (this as DataTableBaseEntity);
            if (entity != null)
            {
                return entity.TableKey;
            }
            Type t = this.GetType();
            foreach (Attribute attr in t.GetCustomAttributes(true))
            {
                TableAttribute tableattribute = attr as TableAttribute;
                if (null != tableattribute)
                {
                    return tableattribute.Guid;
                }
            }
            return "";
        }

        public virtual string GetTableIdValue()
        {
            var entity = (this as DataTableBaseEntity);
            if (entity != null)
            {
                if (((DataTableBaseEntity)entity).DRow[((DataTableBaseEntity)entity).TableKey] == null)
                {
                    return Guid.Empty.ToString();
                }
                return ((DataTableBaseEntity)entity).DRow[((DataTableBaseEntity)entity).TableKey].ToString();
            }
            Type t = this.GetType();
            string id = GetTableId();
            PropertyInfo prop = t.GetProperty(id);
            object value1 = prop.GetValue(this, null);

            return Convert.ToString(value1);
        }

        /// <summary>
        /// 获取当前实体对应的自增ID或整型ID
        /// </summary>
        /// <returns></returns>
        public string GetTableIdentity()
        {
            Type t = this.GetType();
            foreach (Attribute attr in t.GetCustomAttributes(true))
            {
                TableAttribute tableattribute = attr as TableAttribute;
                if (null != tableattribute)
                {
                    return tableattribute.Identity;
                }
            }
            return "";
        }
        
        

        /// <summary>
        /// 实休对象状态[新增，编辑，删除]
        /// </summary>
        public WorkState BaseEntityState
        {
            get;
            set;
        }

        /// <summary>
        /// 是否是Guid![单主键是否是Guid类]
        /// </summary>
        public bool iSun_IsGuid
        {
            get
            {
                return GetTableId() != "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool iSun_Selected
        {
            get;
            set;
        }

        public bool iSun_IsNew
        {
            get;
            set;
        }

        /// <summary>
        /// 表示明细表对应的属性名称
        /// 如果有多个的时候用","分隔开
        /// 如:Detail1,Detail2;
        /// </summary>
        public virtual string DetailObject
        {
            get;
            set;
        }

        #region IComparable Members
        /// <summary>
        /// 数据比较
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if (obj == null)
                return -1;
            if (this.Equals(obj))
                return 0;

            return 1;
        }
        #endregion



    }
}
